package cn.wolfcode.rbac.controller;
// 需求： 要求请求路径为 /role/list

import cn.wolfcode.rbac.domain.Permission;
import cn.wolfcode.rbac.domain.Role;
import cn.wolfcode.rbac.qo.QueryObject;
import cn.wolfcode.rbac.service.IPermissionService;
import cn.wolfcode.rbac.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IPermissionService permissionService;

    // 处理查询所有部门请求
    // 请求路径为：/role/list


    @RequestMapping("/list")
    public String list(Model model, QueryObject qo){
        PageInfo<Role> pageInfo = roleService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "role/list";
    }

    @Autowired
    private Role role;
    // 处理删除请求
    // 请求路径为：/role/delete
    @RequestMapping("/delete")
    public String delete(Long id){
        role.setId(id);
        if (id != null) {
            // 删除中间表（role_permission）
            roleService.delete(role.getId());
            roleService.deleteRelation(role.getId());
        }
        return "redirect:/role/list"; // 重新访问一次/list
    }


    // 处理 去新增 / 去修改
    // 请求路径为：/role/input
    @RequestMapping("/input")
    public String input(Long id, Model model){
        if (id != null) { // 去修改
            Role role = roleService.get(id);
            model.addAttribute("role",role);

            // 查询被修改的角色所有权限数据，存到 Model 中
            List<Permission> ownPermissions = permissionService.queryByRoleId(id);
            model.addAttribute("ownPermissions",ownPermissions);
        }
        // 从数据库中把所有的权限拿出来
        List<Permission> permissions = permissionService.listAll();
        model.addAttribute("permissions",permissions);
        return "role/input"; //
    }

    // 处理 新增 / 修改
    // 请求路径为：/role/delete
    @RequestMapping("/saveOrUpdate")
    public String input(Role role ,Long[] permissionIds){
        if (role.getId() == null) {
            // 往中间role表中存入数据（中间表:role_permission）
            roleService.save(role);
            // 新增
            if(permissionIds != null && permissionIds.length > 0){
                for (Long permissionId : permissionIds) {
                    roleService.insertRelation(role.getId(),permissionId);
                }
            }

        }else {
            // 修改
            roleService.update(role);

            // 先从中间表role_permission 中删除旧有关的数据
            roleService.deleteRelation(role.getId());

            // 再往中间表中role_permission 插入新分配的数据
            if(permissionIds !=null && permissionIds.length>0){
                for (Long permissionId : permissionIds) {
                    roleService.insertRelation(role.getId(),permissionId);
                }
            }
        }
        return "redirect:/role/list"; // 重新访问一次/list
    }
}
